001 /*
002 * Copyright 2004-2005 Stephen J. McConnell.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
013 * implied.
014 *
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package net.dpml.transit.tools;
020
021 import java.io.File;
022 import java.net.URL;
023 import java.net.URI;
024
025 import org.apache.tools.ant.Project;
026 import org.apache.tools.ant.BuildException;
027 import org.apache.tools.ant.taskdefs.ImportTask;
028
029 import net.dpml.transit.Artifact;
030 import net.dpml.util.PropertyResolver;
031
032 /**
033 * Ant task that provides support for the import of build file templates
034 * via an artifact url.
035 *
036 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
037 * @version 1.0.2
038 */
039 public class ImportArtifactTask extends ImportTask
040 {
041 /**
042 * A flag indicating that nested directives have been provided.
043 */
044 private boolean m_flag = false;
045
046 private boolean m_init = false;
047
048 /**
049 * Set the project.
050 * @param project the current project
051 */
052 public void setProject( Project project )
053 {
054 super.setProject( project );
055 setTaskName( "import" );
056 }
057
058 /**
059 * Task initialization.
060 */
061 public synchronized void init()
062 {
063 if( !m_init )
064 {
065 TransitTask.initialize( this );
066 super.init();
067 m_init = true;
068 }
069 }
070
071 // ------------------------------------------------------------------------
072 // Task
073 // ------------------------------------------------------------------------
074
075 /**
076 * Set the file to import. Any symbolic references in the supplied
077 * file argument will be resolved prior to invoking the standard setFile
078 * operation.
079 *
080 * @param file the template filename
081 */
082 public void setFile( String file )
083 {
084 String path = PropertyResolver.resolve( file );
085 super.setFile( path );
086 }
087
088 /**
089 * Set the artifact to import.
090 * @param uri the artifact to import into the build file
091 * @exception BuildException if an error occurs while attempting to
092 * resolve the artifact uri
093 */
094 public void setUri( URI uri ) throws BuildException
095 {
096 try
097 {
098 URL url = Artifact.toURL( uri );
099 File local = (File) url.getContent( new Class[]{File.class} );
100 super.setFile( local.getAbsolutePath() );
101 }
102 catch( Exception e )
103 {
104 final String error =
105 "Could not import the resource from the uri ["
106 + uri
107 + "]";
108 throw new BuildException( error, e );
109 }
110 }
111
112 /**
113 * Execute the import.
114 */
115 public void execute()
116 {
117 try
118 {
119 super.execute();
120 }
121 catch( BuildException e )
122 {
123 throw e;
124 }
125 catch( Throwable e )
126 {
127 final String error =
128 "Input (super).execute() failed.";
129 throw new BuildException( error, e );
130 }
131 }
132 }
133